package com.kkd.kkdapp.adapter;

import java.lang.reflect.Type;
import java.sql.Date;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.kkd.kkdapp.R;
import com.kkd.kkdapp.activity.PaidCreditDetailActivity;
import com.kkd.kkdapp.activity.PaingCreditDetailActivity;
import com.kkd.kkdapp.activity.PaymentActivity;
import com.kkd.kkdapp.http.entity.CrediItem;
import com.kkd.kkdapp.utils.LogUtil;
import com.kkd.kkdapp.view.RoundTextView;
import com.umeng.message.proguard.s;

public class CreditAdapter extends BaseAdapter {

	private List<CrediItem> datalist;
	private Context context;
	private Intent intent;
	private LayoutInflater inflater;
	private SimpleDateFormat df1;
	private DecimalFormat df2;
	private DecimalFormat df3;

	public CreditAdapter(Context context, List<CrediItem> datalist) {
		this.context = context;
		this.datalist = datalist;
		inflater = LayoutInflater.from(context);
		df1 = new SimpleDateFormat("yyyy/MM/dd");
		df2 = new DecimalFormat("#,###.00");
		df3 = new DecimalFormat("0.00");
	}

	@Override
	public int getCount() {
		return datalist.size();
	}

	@Override
	public Object getItem(int arg0) {
		return datalist.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup arg2) {
		ViewHolder holder = null;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = inflater.inflate(R.layout.credit_listview_item, null);
			// 详情
			holder.credit_detail = (RelativeLayout) convertView
					.findViewById(R.id.credit_detail);
			// 放款日期
			holder.credit_date = (TextView) convertView
					.findViewById(R.id.credit_date);
			// 借款金额title
			holder.credit_title = (TextView) convertView
					.findViewById(R.id.credit_title);
			// 借款金额数量
			holder.credit_amount = (TextView) convertView
					.findViewById(R.id.credit_amount);
			// 贷款利率title
			holder.credit_title_left = (TextView) convertView
					.findViewById(R.id.credit_title_left);
			// 每期应还title
			holder.credit_title_middle = (TextView) convertView
					.findViewById(R.id.credit_title_middle);
			// 分期期数title
			holder.credit_title_right = (TextView) convertView
					.findViewById(R.id.credit_title_right);
			// 还款总额
			holder.credit_tv_rate = (TextView) convertView
					.findViewById(R.id.credit_tv_rate);
			// 每期应还
			holder.credit_tv_should = (TextView) convertView
					.findViewById(R.id.credit_tv_should);
			// 期数
			holder.credit_tv_number = (TextView) convertView
					.findViewById(R.id.credit_tv_number);
			// 还款
			holder.credit_next = (TextView) convertView
					.findViewById(R.id.credit_next);
			// 已还清标识图片
			holder.credit_iv_flag = (ImageView) convertView
					.findViewById(R.id.credit_iv_flag);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		CrediItem data = datalist.get(position);
		// 获取条目的信息
		String bidId = data.getBidId();
		long payDate = data.getPayDate();
		long amount = data.getAmount();
		int lilv = data.getLilv();
		long currentPeriodAmount = data.getCurrentPeriodAmount();
		long totalPayAmount = data.getTotalPayAmount();
		int periods = data.getPeriods();
		String status = data.getStatus();
		String date = df1.format(new Date(payDate));
		String amountString = df2.format(amount / (double) 100);
		String totalPayAmountString = df2.format(totalPayAmount / (double) 100);
		String currentPeriodAmountString = df2.format(currentPeriodAmount
				/ (double) 100);
		String lilvString = df3.format(lilv / (double) 100);
		if (".00".equals(currentPeriodAmountString)) {
			currentPeriodAmountString = "0.00";
		}
		if (".00".equals(totalPayAmountString)) {
			totalPayAmountString = "0.00";
		}
		// 设置字体颜色
		if ("4011".equals(status)) {
			// 改变字体颜色为灰色
			holder.credit_date.setTextColor(Color.parseColor("#c2c2c2"));
			holder.credit_title.setTextColor(Color.parseColor("#c2c2c2"));
			holder.credit_amount.setTextColor(Color.parseColor("#c2c2c2"));
			holder.credit_title_left.setTextColor(Color.parseColor("#c2c2c2"));
			holder.credit_title_middle
					.setTextColor(Color.parseColor("#c2c2c2"));
			holder.credit_title_right.setTextColor(Color.parseColor("#c2c2c2"));
			holder.credit_tv_rate.setTextColor(Color.parseColor("#c2c2c2"));
			holder.credit_tv_should.setTextColor(Color.parseColor("#c2c2c2"));
			holder.credit_tv_number.setTextColor(Color.parseColor("#c2c2c2"));
		} else {
			// 改变字体颜色为黑色
			holder.credit_date.setTextColor(Color.parseColor("#666666"));
			holder.credit_title.setTextColor(Color.parseColor("#1a1a1a"));
			holder.credit_amount.setTextColor(Color.parseColor("#ff4e00"));
			holder.credit_title_left.setTextColor(Color.parseColor("#666666"));
			holder.credit_title_middle
					.setTextColor(Color.parseColor("#666666"));
			holder.credit_title_right.setTextColor(Color.parseColor("#666666"));
			holder.credit_tv_rate.setTextColor(Color.parseColor("#666666"));
			holder.credit_tv_should.setTextColor(Color.parseColor("#666666"));
			holder.credit_tv_number.setTextColor(Color.parseColor("#666666"));
		}
		// 设置文字内容
		holder.credit_amount.setText(amountString);
		holder.credit_tv_rate.setText(totalPayAmountString);
		holder.credit_tv_should.setText(currentPeriodAmountString);
		holder.credit_tv_number.setText(periods + "");
		if ("5004".equals(status)) {// 还款中
			// 隐藏已还清标识
			holder.credit_iv_flag.setVisibility(View.GONE);
			// 设置title文字内容
			holder.credit_date.setText("放款日期 " + date);
			// 设置还款背景
			holder.credit_next.setBackgroundResource(R.drawable.orange_selector);
			holder.credit_next.setText("还款");
		} else if ("5005".equals(status)) {// 已还清
			// 显示已还清标识
			holder.credit_iv_flag.setVisibility(View.VISIBLE);
			// 设置title文字内容
			holder.credit_date.setText("放款日期 " + date);
			// 设置还款背景
			holder.credit_next.setBackgroundResource(R.drawable.green_selector);
			holder.credit_next.setText("已还清");
		} else if ("5002".equals(status)) {// 招标中
			// 隐藏已还清标识
			holder.credit_iv_flag.setVisibility(View.GONE);
			// 设置title文字内容
			holder.credit_date.setText("招标中");
			// 设置还款背景
			holder.credit_next.setBackgroundResource(R.drawable.gray);
			holder.credit_next.setText("招标中");
		} else if ("4011".equals(status)) {// 已取消
			// 隐藏已还清标识
			holder.credit_iv_flag.setVisibility(View.GONE);
			// 设置title文字内容
			holder.credit_date.setText("已取消 ");
			// 设置还款背景
			holder.credit_next.setBackgroundResource(R.drawable.gray);
			holder.credit_next.setText("已取消 ");
		} else if ("5003".equals(status)) {// 放款中
			// 隐藏已还清标识
			holder.credit_iv_flag.setVisibility(View.GONE);
			// 设置title文字内容
			holder.credit_date.setText("放款中");
			// 设置还款背景
			holder.credit_next.setBackgroundResource(R.drawable.gray);
			holder.credit_next.setText("放款中");
		}
		holder.credit_next.setPadding(0, 0, 0, 0);
		holder.credit_detail.setOnClickListener(new MyOnclick(status, payDate,
				amount, lilv, currentPeriodAmount, periods, bidId,
				totalPayAmount));
		holder.credit_next.setOnClickListener(new MyOnclick(status, payDate,
				amount, lilv, currentPeriodAmount, periods, bidId,
				totalPayAmount));
		return convertView;
	}

	class MyOnclick implements OnClickListener {

		private String status;
		private long payDate;
		private long amount;
		private int lilv;
		private long currentPeriodAmount;
		private int periods;
		private String bidId;
		private long totalPayAmount;

		public MyOnclick(String status, long payDate, long amount, int lilv,
				long currentPeriodAmount, int periods, String bidId,
				long totalPayAmount) {
			this.status = status;
			this.payDate = payDate;
			this.amount = amount;
			this.lilv = lilv;
			this.currentPeriodAmount = currentPeriodAmount;
			this.periods = periods;
			this.bidId = bidId;
			this.totalPayAmount = totalPayAmount;
		}

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.credit_next: // 　还款
				if ("5004".equals(status)) {// 还款中
					createIntentAndStart(PaingCreditDetailActivity.class,
							payDate, amount, lilv, currentPeriodAmount,
							periods, status, bidId, totalPayAmount);
				} else if ("5005".equals(status)) {// 已还清
					createIntentAndStart(PaingCreditDetailActivity.class,
							payDate, amount, lilv, currentPeriodAmount,
							periods, status, bidId, totalPayAmount);
				}
				break;
			case R.id.credit_detail:
				if ("5004".equals(status)) {// 还款中
					createIntentAndStart(PaingCreditDetailActivity.class,
							payDate, amount, lilv, currentPeriodAmount,
							periods, status, bidId, totalPayAmount);
				} else if ("5005".equals(status)) {// 已还清
					createIntentAndStart(PaingCreditDetailActivity.class,
							payDate, amount, lilv, currentPeriodAmount,
							periods, status, bidId, totalPayAmount);
				}
				break;

			}

		}
	}

	class ViewHolder {
		RelativeLayout credit_detail;
		TextView credit_date, credit_title, credit_amount, credit_title_left,
				credit_title_middle, credit_title_right, credit_tv_rate,
				credit_tv_should, credit_tv_number, credit_next;
		ImageView credit_iv_flag;
	}

	// 实例化intent并跳转
	private void createIntentAndStart(Class<?> type, long payDate, long amount,
			int lilv, long currentPeriodAmount, int periods, String status,
			String bidId, long totalPayAmount) {
		intent = new Intent(context, type);
		intent.putExtra("payDate", payDate);
		intent.putExtra("amount", amount);
		intent.putExtra("lilv", lilv);
		intent.putExtra("currentPeriodAmount", currentPeriodAmount);
		intent.putExtra("periods", periods);
		intent.putExtra("status", status);
		intent.putExtra("bidId", bidId);
		intent.putExtra("totalPayAmount", totalPayAmount);
		context.startActivity(intent);
	}

}
